/**
 * 
 */
package com.jm.web.builder;

import java.io.IOException;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.tags.SPTag;

/**
 * @author yanjie
 *
 * 2011/9/19 上午9:57:47
 * 搜索表单绘制器
 */
public class SPBuilder extends BaseBuilder{
	protected SPTag spTag = null;
	
	@Override
	public void RenderStart() {
		if(this.getBaseTag() instanceof SPTag)
			this.spTag = (SPTag)this.getBaseTag();
		
		this.getBaseTag().getPage().setSpId(this.spTag.getId());
		StringBuilderEx sb = new StringBuilderEx();
		
		if(this.spTag.GetExtValue("layout",true)){
			sb.Append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"sp\"><tr><td>");
			sb.Append("\r\n<div id=\""+this.spTag.getId()+"\" style=\"height: auto;margin-top: 2px;border: %1$s solid #8DB2E3;\" >"
					,this.spTag.GetExtValue("border","1px")+(this.spTag.GetExtValue("border","1px").endsWith("px") ? "" : "px"));
		}
			
		if(this.spTag.getTabSize() > 0){
			sb.Append("\r\n<ul>");
			if(StringHelper.IsNullOrEmpty(this.spTag.getCustomtab())){
				if(this.spTag.getTabSize() > 0)
					sb.Append("\r\n<li  style=\"display:%2$s\" id=\"%1$s_tab0\"><a href=\"#%1$s-1\">基本</a></li>",
							this.spTag.getId(),
							this.spTag.isHiddeTab("tab0") ? "none" : "");
				if(this.spTag.getTabSize() > 1)
					sb.Append("\r\n<li  style=\"display:%2$s\" id=\"%1$s_tab1\"><a href=\"#%1$s-2\">高级</a></li>",
							this.spTag.getId(),
							this.spTag.isHiddeTab("tab1") ? "none" : "");
			}else{
				String strCustomTab = this.spTag.getCustomtab().replaceAll(",","|").replaceAll(";","|");
				String tabs[] = strCustomTab.split("[|]");
				for (int i = 0; i < tabs.length; i++) {
					sb.Append("\r\n<li style=\"display:%4$s\" id=\"%1$s_tab%2$s\"><a href=\"#%1$s-%5$s\">%3$s</a></li>",
							this.spTag.getId(),
							String.valueOf(i),
							tabs[i],
							this.spTag.isHiddeTab("tab"+i) ? "none" : "",
									String.valueOf(i+1));
				}
			}
			sb.Append("\r\n</ul>");
		}
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.jm.web.builder.BaseBuilder#Render()
	 */
	@Override
	public void RenderEnd() {
		if(this.getBaseTag() instanceof SPTag)
			this.spTag = (SPTag)this.getBaseTag();
		
		StringBuilderEx sb = new StringBuilderEx();
		if(this.spTag.GetExtValue("layout",true)){
			sb.Append("</div>");
			sb.Append("</td></tr></table>");
			try {
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(this.spTag.getTabSize() > 0 || !StringHelper.IsNullOrEmpty(this.spTag.getCustomtab())){
			int tabSize = 0;
			if(StringHelper.IsNullOrEmpty(this.spTag.getCustomtab())){
				tabSize = this.spTag.getTabSize();
			}else{
				String strCustomTab = this.spTag.getCustomtab().replaceAll(",","|").replaceAll(";","|");
				String tabs[] = strCustomTab.split("[|]");
				tabSize = tabs.length;
			}
			
			//註冊搜索表單標籤頁配置的JS
			sb.Reset();
			sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"sp-tab"));
			sb.Append("$P.hidetabs['%1$s']='%2$s';",
					this.spTag.getId(),
					this.spTag.GetExtValue("hideTabs",""));
			
			sb.Append("var config={};");
			if(this.spTag.GetExtValue("cache",true) || this.spTag.GetExtValue("hideTabs","").length() > 0){
				sb.Append("var tabindex = $P.getVtabid('%1$s',$.cookie('tabindex_%1$s'),%2$s);if(tabindex != null) config['selected']=tabindex;",
				this.spTag.getId(),
				String.valueOf(tabSize));
			}
			sb.Append("config['select']=function(event, ui){$P.tabselect(ui,'%1$s',%2$s)}; ",this.spTag.getId(),this.spTag.GetExtValue("cache",true));			
			sb.Append("$P.tabs['%1$s'] = new $P.dptab($('div[id=%1$s]').tabs(config),'%1$s',%2$s);"
					,this.spTag.getId(),
					tabSize);
			sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"sp-tab"));
			
			try {
				if(!this.getBaseTag().getPage().isExport()){
					this.getOut().write("<script>if(true){"+sb.toString()+"}</script>");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			//this.spTag.getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_TABS, sb.toString());
		}
		
		//注册表格页面默认回车事件
		if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("keydownconfig",""))){
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, 
					StringHelper.Format("$P.form.keydownConfig={%1$s};$P.form.regKeydownEvent();",this.getBaseTag().GetExtValue("keydownconfig","")));
		}
	}
}
